package com.wm.task.client.view;

import com.wm.task.client.dao.StudentNetDAO;
import com.wm.task.common.vo.Student;

import java.time.LocalDate;
import java.util.List;

public class MainView extends BaseView {
    private String title = "学生管理系统V3.0";
    private String[] menus = {"添加学生", "显示所有学生", "根据姓名查找学生", "根据生日查找学生", "删除学生", "统计学生总人数", "统计男生人数", "统计女生人数"};
    private StudentNetDAO studentNetDAO = StudentNetDAO.getInstance();

    @Override
    protected void handleChoice(String choice) {
        switch (choice) {
            case "1": //添加学生
                handleAdd();
                break;
            case "2": //显示所有学生
                handleList();
                break;
            case "3"://姓名查找
                handleSearchName();
                break;
            case "4"://生日查找
                handleSearchBirthday();
                break;
            case "5"://删除学生
                handleDelete();

                break;
            case "6"://统计总人数
                handleCount();

                break;
            case "7"://男生人数
                handleCountMan();

                break;
            case "8"://女生人数
                handleCountWomen();

                break;
        }
    }

    private void handleCountWomen() {
        System.out.println("---统计学生数(女)---");
        System.out.println("总共有" + studentNetDAO.handleCountWomen(studentNetDAO.findAll()) + "名女生");
    }

    private void handleCountMan() {
        System.out.println("---统计学生数(男)---");
        System.out.println("总共有" + studentNetDAO.handleCountMan(studentNetDAO.findAll()) + "名男生");
    }

    private void handleCount() {
        System.out.println("---统计学生数(总)---");
        List<Student> students = studentNetDAO.findAll();
        System.out.println("总共有" + students.size() + "名学生");
    }

    private void handleDelete() {
        System.out.println("---删除学生(名字)---");
        System.out.print("请输入学生姓名:");
        String name = scanner.next();
        if (studentNetDAO.handleDelete(name)) {
            System.out.println("删除成功!!");
        } else {
            System.out.println("删除失败!!");
        }

    }

    private void handleSearchBirthday() {
        System.out.println("---查找学生(生日)---");
        System.out.println("输入起始生日");
        System.out.print("年:");
        int year = scanner.nextInt();
        System.out.print("月:");
        int month = scanner.nextInt();
        System.out.print("日:");
        int day = scanner.nextInt();
        LocalDate birthdayStart = LocalDate.of(year, month, day);

        System.out.println("输入终止生日");
        System.out.print("年:");
        year = scanner.nextInt();
        System.out.print("月:");
        month = scanner.nextInt();
        System.out.print("日:");
        day = scanner.nextInt();
        LocalDate birthdayEnd = LocalDate.of(year, month, day);
        List<Student> studentList = studentNetDAO.findBirthday(birthdayStart, birthdayEnd);
        if (studentList == null) {
            System.out.println("未查找到该学生");
            return;
        }
        for (int i = 0; i < studentList.size(); i++) {
            Student st = studentList.get(i);
            System.out.printf("%d、%s[%s]%s\n", i + 1, st.getName(), st.getSex(), st.getBirthday());
        }
    }

    private void handleSearchName() {
        System.out.println("---查找学生(名字)---");
        System.out.print("请输入学生姓名:");
        String name = scanner.next();
        Student st = studentNetDAO.findName(name);
        if (st == null) {
            System.out.println("未查找到该学生");
            return;
        }
        System.out.printf("%d、%s[%s]%s\n", st.getId(), st.getName(), st.getSex(), st.getBirthday());

    }

    private void handleAdd() {
        Student student = new Student();
        System.out.println("---添加学员---");

        String name;
        do {
            System.out.print("输入姓名:");
            name = scanner.next();
        } while (studentNetDAO.check(name));
        student.setName(name);

        System.out.print("输入性别:");
        String sex = scanner.next();
        student.setSex(sex);

        System.out.println("输入生日:");
        System.out.print("年:");
        int year = scanner.nextInt();
        System.out.print("月:");
        int month = scanner.nextInt();
        System.out.print("日:");
        int day = scanner.nextInt();
        student.setBirthday(LocalDate.of(year, month, day));

        studentNetDAO.save(student);
    }

    private void handleList() {
        System.out.println("---列表----");
        List<Student> students = studentNetDAO.findAll();
        for (int i = 0; i < students.size(); i++) {
            Student st = students.get(i);
            System.out.printf("%d、%s[%s]%s\n", i + 1, st.getName(), st.getSex(), st.getBirthday());
        }
    }

    @Override
    protected String getTitle() {
        return title;
    }

    @Override
    protected String[] getMenus() {
        return menus;
    }
}
